/*
 * Copyright 2011 Clean GWT
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package com.cleangwt.view.client;

import java.util.List;

import com.cleangwt.json.client.SimpleObject;
import com.google.gwt.view.client.ListDataProvider;
import com.google.gwt.view.client.ProvidesKey;

/**
 * The {@link ListDataProvider} for type value of {@link SimpleObject}.
 * 
 * @author Jess
 * @version 1.0
 * @since 1.0
 */
public class SimpleDataProvider extends ListDataProvider<SimpleObject> {

  /**
   * Creates an empty model.
   */
  public SimpleDataProvider() {
    super();
  }

  /**
   * Creates a list model that wraps the given list.
   * 
   * <p>
   * The wrapped list should no longer be modified as the data provider cannot detect changes to the
   * wrapped list. Instead, call {@link #getList()} to retrieve a wrapper that can be modified and
   * will correctly forward changes to displays.
   * 
   * @param listToWrap the List to be wrapped
   */
  public SimpleDataProvider(List<SimpleObject> listToWrap) {
    super(listToWrap);
  }

  /**
   * Creates a list model that wraps the given list.
   * 
   * <p>
   * The wrapped list should no longer be modified as the data provider cannot detect changes to the
   * wrapped list. Instead, call {@link #getList()} to retrieve a wrapper that can be modified and
   * will correctly forward changes to displays.
   * 
   * @param listToWrap the List to be wrapped
   * @param keyProvider an instance of ProvidesKey<SimpleObject>, or null if the record object
   *          should act as its own key
   */
  public SimpleDataProvider(List<SimpleObject> listToWrap, ProvidesKey<SimpleObject> keyProvider) {
    super(listToWrap, keyProvider);
  }

  /**
   * Creates an empty list model that wraps the given collection.
   * 
   * @param keyProvider an instance of ProvidesKey<SimpleObject>, or null if the record object
   *          should act as its own key
   */
  public SimpleDataProvider(ProvidesKey<SimpleObject> keyProvider) {
    super(keyProvider);
  }

  /**
   * Removes list data via the key of value.
   * 
   * @param value the value
   */
  public void remoteListData(SimpleObject value) {
    List<SimpleObject> list = getList();
    Object key = getKey(value);
    for (int i = 0; i < list.size(); i++) {
      if (key.equals(getKey(list.get(i)))) {
        list.remove(i);
        break;
      }
    }
  }

  /**
   * Updates list data via the key of value.
   * 
   * @param value the value
   */
  public void updateListData(SimpleObject value) {
    List<SimpleObject> list = getList();
    Object key = getKey(value);
    for (int i = 0; i < list.size(); i++) {
      if (key.equals(getKey(list.get(i)))) {
        list.set(i, value);
        break;
      }
    }
  }

}
